﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Collections;

namespace TCL.FUNC
{
    public class WebFormEx
    {
        #region "CKEditorControl"
        public static void LoadCKEditor(CKEditor.NET.CKEditorControl ControlId)
        {
            ControlId.config.toolbar = new object[] { 
              new object[] { "Source"}
            };
        }
        #endregion

        #region "CheckBoxList"
        public static String getCheckBoxList_Selected(CheckBoxList cblControl, Boolean isValue)
        {
            string selectedValue = "";
            foreach (ListItem item in cblControl.Items)
            {
                if (item.Selected)
                    if (isValue)
                        selectedValue = selectedValue + "," + item.Value;
                    else
                        selectedValue = selectedValue + "," + item.Text;
            }
            if (selectedValue.Length > 1)
            {
                selectedValue = "," + selectedValue + ",";
                selectedValue = System.Text.RegularExpressions.Regex.Replace(selectedValue, @"\s+", " ").Trim(); // convert multiple spaces into one space
                return System.Text.RegularExpressions.Regex.Replace(selectedValue, @",+", ",").Trim(); // convert multiple ',,,' to ','
            }
            else
                return selectedValue + ",";
        }

        public static void setCheckBoxList_Selected(CheckBoxList cblControl, String sListValue)
        {
            if (!String.IsNullOrEmpty(sListValue))
            {
                string[] key = sListValue.Split(',');
                foreach (ListItem item in cblControl.Items)
                {
                    if (key.Contains(item.Value))
                        item.Selected = true;
                    else
                        item.Selected = false;
                }
            }
            else
            {
                foreach (ListItem item in cblControl.Items)
                {
                    item.Selected = false;
                }
            }
        }
        #endregion

        #region "DropDownList"
        public static String getDropDownList_SelectValue(DropDownList ddlControl, Boolean isValue)
        {
            string selectedValue = "";
            if (isValue)
                selectedValue = ddlControl.SelectedValue;
            else
                selectedValue = ddlControl.SelectedItem.Text;
            return selectedValue;
        }

        public static void setDropDownList_SelectValue(DropDownList ddlControl, String sValue)
        {
            if (String.IsNullOrEmpty(sValue))
            {
                ddlControl.SelectedIndex = 0;
                return;
            }
            if (sValue == "0")
            {
                ddlControl.SelectedIndex = 0;
                return;
            }
            else
                ddlControl.SelectedValue = sValue;
        }

        public static void setDropDownList_DataSource(DropDownList ddlControl, SortedList stlData)
        {
            ddlControl.DataSource = stlData;
            ddlControl.DataValueField = "Key";
            ddlControl.DataTextField = "Value";
            ddlControl.DataBind();
        }
        #endregion

        #region "TextBox"
        public static void setTextBox_Text_DateTime(TextBox txtControl, DateTime tValue, Boolean isFullDateTime = true)
        {
            if (tValue == null || tValue == DateTime.MinValue)
            {
                if (isFullDateTime)
                    txtControl.Text = DateTime.Now.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5);
                else
                    txtControl.Text = DateTime.Now.ToString(FormatEx.Date_Time.Format_Date_Html5);
                return;
            }
            else
                if (isFullDateTime)
                    txtControl.Text = tValue.ToString(FormatEx.Date_Time.Format_DateTime_Local_Html5);
                else
                    txtControl.Text = tValue.ToString(FormatEx.Date_Time.Format_Date_Html5);
        }

        public static void setTextBox_Text(TextBox txtControl, String sValue, String sDefault)
        {
            if (String.IsNullOrEmpty(sValue))
            {
                txtControl.Text = sDefault;
                return;
            }
            else
                txtControl.Text = sValue;
        }
        #endregion

        //public static string getListKey_Save_Database(String lstKey)
        //{
        //    lstKey = "," + StringEx.Trim(lstKey) + ",";
        //    lstKey = System.Text.RegularExpressions.Regex.Replace(lstKey, @"\s+", " ").Trim(); // convert multiple spaces into one space
        //    return System.Text.RegularExpressions.Regex.Replace(lstKey, @",+", ",").Trim();// convert multiple ',,,' to ','
        //}
    }
}
